package com.zcy.basic.service.impl;

import com.zcy.basic.util.PageInfo;
import com.zcy.basic.util.PageList;
import com.zcy.basic.mapper.BaseMapper;
import com.zcy.basic.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author lucker
 * @date 2023/11/30 14:34
 */
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> listAll() {
        return baseMapper.listAll();
    }

    @Transactional
    @Override
    public Long add(T t) {
        return baseMapper.add(t);
    }

    @Transactional
    @Override
    public int deleteById(Serializable id) {
        return baseMapper.deleteById(id);
    }

    @Transactional
    @Override
    public int deleteBatch(List<Long> tIdList) {
        return baseMapper.deleteBatch(tIdList);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T queryById(Serializable id) {
        return baseMapper.queryById(id);
    }

    @Override
    public PageList<T> queryPage(PageInfo pageInfo) {
        Long total = baseMapper.queryPageTotal(pageInfo);
        List<T> tList = new ArrayList<>();
        if (total > 0L) {
            if (pageInfo.getStart() >= total && pageInfo.getCurrentPage() > 1) {
                pageInfo.setCurrentPage(pageInfo.getCurrentPage() - 1);
            }
            tList = baseMapper.queryPage(pageInfo);
        }
        return new PageList<>(total, tList);
    }
}
